package widget

import (
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
)

type FunCreatWidget func(baseWidgetName string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem

type WidgetFunInfo struct {
	fn             FunCreatWidget
	Name           string
	baseWidgetName string
}

var AllWidget = map[string]*WidgetFunInfo{}

func RegWidget(baseWidgetName, Name string, fn FunCreatWidget) {
	if AllWidget == nil {
		AllWidget = make(map[string]*WidgetFunInfo)
	}
	AllWidget[baseWidgetName] = &WidgetFunInfo{
		fn:             fn,
		Name:           Name,
		baseWidgetName: baseWidgetName,
	}
}
func getWidgetFunction(baseWidgetName string) FunCreatWidget {
	if AllWidget == nil {
		return nil
	}
	o, ok := AllWidget[baseWidgetName]
	if !ok {
		return nil
	}
	return o.fn
}

func CreatWidgetByName(name string, baseWidgetName string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	fn := getWidgetFunction(baseWidgetName)
	if fn == nil {
		return nil
	}
	return fn(name, skinInfo, rootNode)
}
func CheckWidgetName(baseWidgetName string) bool {
	fn := getWidgetFunction(baseWidgetName)
	if fn == nil {
		return false
	}
	return true
}

////////////////////////////////////////////////////////////////////////////////

const (
	Default_WidgetContainer_Name = "EmptyWidgetContainer"
)

type FunCreatWidgetContainer func(size fyne.Size) widgetcommon.IWidgetItem

var AllWidgetContainer = map[string]FunCreatWidgetContainer{}

func RegWidgetContain(skinName string, fn FunCreatWidgetContainer) {
	if AllWidgetContainer == nil {
		AllWidgetContainer = make(map[string]FunCreatWidgetContainer)
	}
	AllWidgetContainer[skinName] = fn
}
func getWidgetContain(skinName string) FunCreatWidgetContainer {
	if AllWidgetContainer == nil {
		return nil
	}
	return AllWidgetContainer[skinName]
}

func CreatWidgetContainerByName(name string, size fyne.Size) widgetcommon.IWidgetItem {
	fn := getWidgetContain(name)
	if fn == nil {
		return nil
	}
	return fn(size)
}
